package neusoft.edu.cn.a215lmeordering.model;

import java.util.List;

import neusoft.edu.cn.a215lmeordering.Listener.ListListener;
import neusoft.edu.cn.a215lmeordering.bean.Food;
import neusoft.edu.cn.a215lmeordering.service.AllService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class FoodSearchModel {

    private Retrofit retrofit;

    public FoodSearchModel(){
        retrofit = new Retrofit.Builder()
                .baseUrl("http://172.24.10.175:8080/foodService/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    public void getResultList(String search ,final ListListener shopListListener){

        AllService shopListService = retrofit.create(AllService.class);

        Call<List<Food>> shopCall = shopListService.getFoodBySearch(search);

        shopCall.enqueue(new Callback<List<Food>>() {
            @Override
            public void onResponse(Call<List<Food>> call, Response<List<Food>> response) {
                if(response!=null)
                        shopListListener.onResponse(response.body());
                    else shopListListener.onFail("获取商店列表失败");
            }

            @Override
            public void onFailure(Call<List<Food>> call, Throwable t) {
                shopListListener.onFail(t.toString());
            }
        });

    }
}
